import 'package:eshi/app/CommonFunctions.dart';
import 'package:eshi/app/shi/data/System.dart';
import 'package:flutter/material.dart';

Widget simpleTabs(Map<String,List<Widget>> marks){
    var size=marks.length;
    List<Widget> tabTitles=marks.keys.map((v){return Text(v);}).toList();
    List<Widget> tabBodys = marks.values.map((v){
      return Column(children: v);
    }).toList();
    return DefaultTabController(
       length: size,
       child: Column(children: [
         TabBar(
             tabs:  tabTitles
         ),
         TabBarView(
             children: tabBodys,
        ),
       ]),
    );
  }


void modeWindow(BuildContext context,Widget body){
  if(System.isPhone){
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => Scaffold(
          // appBar: AppBar(title: Text(title)),
          body: body,
        ),
      ),
    );
  }else{
     //自定义编辑本界面内容 
      showDialog(context: context, builder: (BuildContext context) { 
        return Dialog(
          // 自定义Dialog的形状，这里设置了10.0的圆角半径
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10.0),
          ),
          child:body,
        );
      });
  }
}

 simpleDialog({
  required BuildContext context,
  required Widget body,
  String title="",   //标题
  bool scrollable=false, //内容可滚动
  bool barrierDismissible =true,  // 周边单击关闭
  }) async{
   await showDialog(
     context: context,
     barrierDismissible:barrierDismissible, 
     builder: (BuildContext context) { 
        return AlertDialog(
          // 去除内容区域的内边距
          contentPadding: EdgeInsets.zero,
          scrollable:scrollable,
          // 自定义Dialog的形状，这里设置了10.0的圆角半径
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10.0),
          ),
          title: title.isNotEmpty ? Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(title),
              InkWell(child: Icon(Icons.close),onTap: ()=> Navigator.pop(context) )
            ]
          ) : null,
          content:  Column(
            mainAxisAlignment: MainAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            
            children: [
               body
            ],
          ),
        );
    });
}

simpleSetting({required BuildContext context,required  List<String> values,String title="配置", selectValue,required Function onChange}){
  showModalBottomSheet(
      context: context,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
      ),
      clipBehavior: Clip.antiAlias,
      builder: (context) =>Container(
        padding: const EdgeInsets.all(16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // 标题
            Padding(
              padding: EdgeInsets.only(bottom: 16),
              child: mText(
                title,
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            
            // 语言选项列表
            Expanded(
              child: ListView.builder(
                 shrinkWrap: true,
                itemCount: values.length,
                itemBuilder: (context, index) {
                  final item = values[index];
                  final isSelected = selectValue == item;
                  
                  return ListTile(
                    // 国旗图标 + 语言名称
                    // leading: mText(
                    //   language.icon,
                    //   style: const TextStyle(fontSize: 24),
                    // ),
                    title: mText(item),
                    // 选中状态指示
                    trailing: isSelected
                        ? const Icon(Icons.check, color: Colors.blue)
                        : null,
                    // 点击选择语言
                    onTap: () {
                      onChange(item,index);
                      // _saveLanguageSelection(language);
                      Navigator.pop(context); // 关闭底部弹窗
                    },
                  );
                },
              )
            )
        ])
      )
  );
}


// 长按回调：显示菜单
  void simpleMenu({required BuildContext context,required Offset position,required List<String> items,Function? onChange}) async {
    // 菜单选项
    final result = await showMenu(
      context: context,
      // 菜单显示位置（基于长按的坐标）
      position: RelativeRect.fromLTRB(
        position.dx, // 左坐标
        position.dy, // 上坐标
        MediaQuery.of(context).size.width - position.dx,
        MediaQuery.of(context).size.height - position.dy,
      ),
      items: [
          for(var it in items)
           PopupMenuItem(
            value: it,
            child: Row(
              children: [
                // Icon(Icons.copy, size: 18, color: Colors.grey),
                SizedBox(width: 8),
                mText(it),
              ],
            ),
          ), 
        // 菜单项1
        // PopupMenuItem(
        //   value: 'copy',
        //   child: Row(
        //     children: const [
        //       Icon(Icons.copy, size: 18, color: Colors.grey),
        //       SizedBox(width: 8),
        //       Text('复制'),
        //     ],
        //   ),
        // ),
        // // 菜单项2
        // PopupMenuItem(
        //   value: 'share',
        //   child: Row(
        //     children: const [
        //       Icon(Icons.share, size: 18, color: Colors.grey),
        //       SizedBox(width: 8),
        //       Text('分享'),
        //     ],
        //   ),
        // ),
        // // 菜单项3（带分割线）
        // const PopupMenuDivider(height: 1),
        // PopupMenuItem(
        //   value: 'delete',
        //   child: Row(
        //     children: const [
        //       Icon(Icons.delete, size: 18, color: Colors.red),
        //       SizedBox(width: 8),
        //       Text('删除', style: TextStyle(color: Colors.red)),
        //     ],
        //   ),
        // ),
      ],
      // 菜单背景色
      color: Colors.white,
      // 菜单形状（圆角）
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(8),
        side: BorderSide(color: Colors.grey[200]!),
      ),
    );

    // 处理菜单选择结果
    if (result != null && onChange!=null) {
      onChange(result);
    }
  }